/*******LJL 20150820*******/
var CurElemTypeArray = [];
var curmodelarray = [];
var xotreemodel = new XML.ObjTree();
var GetCurrModelObj;
var dumperdev = new JKL.Dumper();
var tableIds = [];

/****Jquery加载事件****/
$(function () {
	//获取sysParam xml
	GetparamTypexmlTjson();
	//获取model xml
	GetDevModelxmlTjson();
	
});

/**** 获取设备类型名称****/
function GetDtypeNameByCurrCtype(currctype, currdtype) {
	if (currctype == 1) {
		return LONinterfaceTypeMap.get(currdtype);
	} else if (currctype == 2) {
		return l880interfaceTypeMap.get(currdtype);
	} else if (currctype == 4) {
		return NICODMX512interfaceTypeMap.get(currdtype);
	} else {
		return null;
	}
}

/**** 获取模板xml****/
function GetDevModelxmlTjson() {
	$.ajax({
		url : '../orgMgr/devCfg/loadDevModelCfg',
		type : 'get',
		dataType : 'text',
		timeout : 100000,
		cache : false,
		data : {
			orgId : parent.CurrorgID
		},
		error : function () {
			alert("获取参数超时");
			return false;
		},
		success : function (dataxml) {
			if (dataxml == "") {
				isCfgNotNull = false;
			} else {
				var tree = xotreemodel.parseXML(dataxml);
				GetCurrModelObj = $.parseJSON(dumperdev.dump(tree));
				if (ParseModelCfgJSONData() == true) { 
					selectetypechange();
					console.log("初始化解析后的CurElemTypeArray:", CurElemTypeArray);
				}
			}
		}
	})
}

/****xml数据解析****/
function ParseModelCfgJSONData() {
	var curmodelarray = [];
	if (!GetCurrModelObj.paramTypes.hasOwnProperty("elemType")) {
		return true;
	}
	CurElemTypeArray = [];
	var newelemType = GetCurrModelObj.paramTypes.elemType;
	if (typeof(newelemType) == "object" && newelemType.constructor == Array) { //多个elemType
		curmodelarray = [];
		$.each(newelemType, function (indexnewelemType, itemnewelemType) {
			var elemtype = itemnewelemType.etype;
			var elemname = itemnewelemType.name;
			//CurElemTypeArray.push({"etype":elemtype,"name":elemname});
			if (itemnewelemType.hasOwnProperty("model")) {
				var newmodel = itemnewelemType.model;
				if (typeof(newmodel) == "object" && newmodel.constructor == Array) { //多个model
					curmodelarray = []; ////////////////jia
					$.each(newmodel, function (indexnewmodel, itemnewmodel) {
						var mid = itemnewmodel.id;
						var modelmodel = itemnewmodel.model;
						if (itemnewmodel.hasOwnProperty("cmd")) {
							var newcmd = itemnewmodel.cmd;
							curmodelarray.push({
								"id" : mid,
								"model" : modelmodel,
								"cmd" : newcmd
							}); ////////jia
							//return true;
						} else { //无cmd
							curmodelarray.push({
								"id" : mid,
								"model" : modelmodel
							}); ////////jia
							//return true;
						}
					});
				} else { //一个model
					//newmodel
					curmodelarray = [];
					if (newmodel.hasOwnProperty("cmd")) { //有cmd
						var newcmd = newmodel.cmd;
						curmodelarray.push({
							"id" : newmodel.id,
							"model" : newmodel.model,
							"cmd" : newcmd
						}); ////////jia
						//return true;
					} else { //无cmd
						curmodelarray.push({
							"id" : newmodel.id,
							"model" : newmodel.model
						}); ////////jia
						//return true;
					}
				}
			}
			CurElemTypeArray.push({
				"etype" : elemtype,
				"name" : elemname,
				"model" : curmodelarray
			});
			return true;
		});
		return true;
	} else { //一个elemType
		//newelemType
		var elemtype = newelemType.etype;
		var elemname = newelemType.name;
		if (newelemType.hasOwnProperty("model")) {
			var newmodel = newelemType.model;
			if (typeof(newmodel) == "object" && newmodel.constructor == Array) { //多个model
				curmodelarray = [];
				$.each(newmodel, function (indexnewmodel, itemnewmodel) {
					var mid = itemnewmodel.id;
					var modelmodel = itemnewmodel.model;
					if (itemnewmodel.hasOwnProperty("cmd")) { //has cmd
						var newcmd = itemnewmodel.cmd;
						curmodelarray.push({
							"id" : mid,
							"model" : modelmodel,
							"cmd" : newcmd
						});
					} else { //无cmd
						curmodelarray.push({
							"id" : mid,
							"model" : modelmodel
						});
					}
				});
			} else { //一个model
				//newmodel
				curmodelarray = [];
				if (newmodel.hasOwnProperty("cmd")) { //有cmd
					var newcmd = newmodel.cmd;
					curmodelarray.push({
						"id" : newmodel.id,
						"model" : newmodel.model,
						"cmd" : newcmd
					});
					//return true;
				} else { //无cmd
					curmodelarray.push({
						"id" : newmodel.id,
						"model" : newmodel.model
					});
				}
			}
		}
		CurElemTypeArray.push({
			"etype" : elemtype,
			"name" : elemname,
			"model" : curmodelarray
		});
		return true;
	}
	return true;
}